package com.yanqu.road.server.manager.activity.snowgarden;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.snowgarden.config.SnowGardenBuyConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.snowgarden.service.CrossMallBuyAddServiceCmd;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.sync.message.Service;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossActivityMallDayBuyMgr extends TempCrossMgr {
    private static Map<Integer, CrossMallDayBuyActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.SnowGarden.getValue());
        activityTypeList.add(eActivityType.SummerGames.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        List<Integer> activityIdList = new ArrayList<>();
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadActivity(item);
            if(item.getType() == eActivityType.SnowGarden.getValue()){
                activityIdList.add(item.getActivityId());
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean removeExpireData() throws Exception {
        long currentSecond = DateHelper.getCurrentTime() / 1000;
        List<Integer> needRemoveList = new ArrayList<>();
        for (CrossMallDayBuyActivity activity : activityMap.values()) {
            int activityId = activity.getActivityId();
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
            if (activityInfo == null || currentSecond > activityInfo.getEndShowTime() + 5 * DateHelper.MINUTE_SECONDS){
                needRemoveList.add(activityId);
            }
        }

        for (Integer activityId : needRemoveList) {
            activityMap.remove(activityId);
        }

        return true;
    }

    @Override
    public boolean save() {
        for (CrossMallDayBuyActivity activity : activityMap.values()) {
            activity.save();
        }
        return true;
    }

    public static void initService(Service syncMessageService) {
        syncMessageService.addCrossServiceCommand(CrossProtocol.C_SEVER_MALL_BUY_ADD, new CrossMallBuyAddServiceCmd());
    }

    public static void reloadActivity(ActivityInfo item) {
        int activityId = item.getActivityId();
        CrossMallDayBuyActivity activity = new CrossMallDayBuyActivity(activityId);
        activityMap.put(activityId, activity);
    }

    public static CrossMallDayBuyActivity getActivity(int activityId) {
        CrossMallDayBuyActivity activity = activityMap.get(activityId);
        if (activity == null) {
            synchronized (activityMap) {
                activity = activityMap.get(activityId);
                if (activity == null) {
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (activityInfo == null) {
                        return null;
                    }
                    activity = new CrossMallDayBuyActivity(activityId);
                    activityMap.put(activityId, activity);
                }
            }
        }
        return activity;
    }

    public static void updateServerMallBuy(int activityId, long serverId, int day, int mallId, long count) {
        CrossMallDayBuyActivity activity = getActivity(activityId);
        if (activity == null) {
            return;
        }
        activity.updateServerMallBuy(serverId, day, mallId, count);
    }

    public static void syncDataTask() {
        for (CrossMallDayBuyActivity activity : activityMap.values()) {
            activity.syncServerMallBuyToGameServer();
            activity.checkDayAutoRewardTask();
            activity.checkFakeBuyTask();
        }
    }
}
